package tech.eyao.hs60x.exception;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.eyao.hs60x.dto.BaseResult;
import tech.eyao.hs60x.enums.ExceptionCodeEnum;

@ControllerAdvice
public class GlobalExceptionHandler {
	private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public BaseResult allExceptionHandler(HttpServletRequest request, Exception exception) throws Exception {
    	logger.error("exception method:" + request.getRequestURL().toString() + " query:" + request.getQueryString(),exception);
    	BaseResult result = new BaseResult(ExceptionCodeEnum.SYSTEM_EXCEPTION.getCode(),
                exception instanceof Hs60xException ? exception.getMessage():ExceptionCodeEnum.SYSTEM_EXCEPTION.getMessage());
        return result;
    }
}
